
package ustc.browser;

import java.util.ArrayList;

import android.app.Activity;

public class TabControl {
//This class uses SingleTon Pattern	
	
	private static	TabControl 	mInstance = null;
	//Maximum number of web views 
	private static	int			MAX_TABS_SIZE = 9;
	private ArrayList<Tab> 		mTabList = null;
	//The activity to which this class belongs
	private Activity			mActivity = null;
	private	int					mCurrentTabIndex;
	
	private TabControl(Activity act){
		mActivity = act;
		mTabList = new ArrayList<Tab>(MAX_TABS_SIZE);
		mCurrentTabIndex = -1;
	}
	
	public static TabControl getTabControl(Activity act){
		if(null == mInstance){
			mInstance = new TabControl(act);
		}
		return	mInstance;
	}
	
	public Tab createNewTab(){
	//Desc: Create a new Tab and add it into mTabList.
	//Pre:  Tabs in mTabList less than MAX_TABS_SIZE.
	//Post: Under precondition the newly-created Tab
	//		will be added into mTabList and returned,
	//		or return null.
		if(getTabSize() < MAX_TABS_SIZE){
			Tab t = new Tab(mActivity);
			mTabList.add(t);
			//New tab is now the "current" tab
			mCurrentTabIndex = mTabList.size()-1;
			t = null;
			return	getCurrentTab();
		}
		return	null;
	}
	
	public Tab getCurrentTab(){
		return	mTabList.get(mCurrentTabIndex);
	}
	
	public int getTabSize(){
	//Desc: Return count of the tabs
		return	mTabList.size();
	}
	
	public Tab getTabOfIndex(int index){
	//Desc: Get the Tab associated with the index.
	//Pre: 0<=index<=mTabList.size()-1
	//Post: Return the corresponding Tab
		if( 0 <= index && index <= mTabList.size()-1){
			return	mTabList.get(index);
		}else{
			return	 null;
		}
	}
	
	public boolean removeTabOfIndex(int index){
	//Desc: Remove the Tab associated with the index.
	//Pre:	0<=index<=mTabList.size()-1
	//Post:	Remove the corresponding Tab and reset
	//		mCurrentTabIndex if necessary.
		if( 0<=index && index<= mTabList.size()-1){
			mTabList.remove(index);
			if(mCurrentTabIndex == index)
				mCurrentTabIndex = 0;
			return	true;
		}
		else 
			return false;
	}
	
	public boolean isEmpty(){
	//DESC: Return true if mTabList is empty.
		return	(0 == mTabList.size());
	}
}